//
//  AddTimeListSectionListHeader.m
//  Doctor
//
//  Created by 阳光 on 16/10/10.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "AddTimeListSectionListHeader.h"

@interface AddTimeListSectionListHeader()

// 箭头
@property(strong,nonatomic)UIButton *arrowButton;

@end

@implementation AddTimeListSectionListHeader

+ (instancetype)headerView:(UITableView *)tableView
{
    static NSString *identifier=@"header";
    AddTimeListSectionListHeader *headerView=(AddTimeListSectionListHeader *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!headerView){
        headerView=[[AddTimeListSectionListHeader alloc] initWithReuseIdentifier:identifier];
    }
    
    return headerView;
}

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithReuseIdentifier:reuseIdentifier];
    if (self)
    {
        // 添加一条底线
        UIView *linesView=[[UIView alloc]initWithFrame:CGRectMake(0,selectViewH-2,APPW,2)];
        linesView.backgroundColor=MAIN_BACK_COLOR;
        [self addSubview:linesView];
        
        _arrowBtn=[UIButton new];
        [_arrowBtn setImage:[UIImage imageNamed:@"cell_arrow"] forState:0];
        [self addSubview:self.arrowBtn];
        [_arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-kmargin);
            make.size.mas_equalTo(CGSizeMake(kmargin*2,kmargin*2));
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            
        }];
        [_arrowBtn addTarget:self action:@selector(arrowBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)arrowBtnEvent:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(arrowBtnEvent:)])
    {
        [self.delegate arrowBtnEvent:btn];
    }
}

#pragma mark - 懒加载
- (UIButton *)arrowButton
{
    if(!_arrowButton)
    {
        _arrowButton =[UIButton buttonWithType:UIButtonTypeCustom];
        [_arrowButton setBackgroundImage:[UIImage imageNamed:@"header_bg"] forState:UIControlStateNormal];
        
        [_arrowButton setBackgroundImage:[UIImage imageNamed:@"header_bg_highlighted"] forState:UIControlStateHighlighted];
        [self addSubview:self.arrowButton];
        [_arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.mas_equalTo(0);
            make.width.mas_equalTo(APPW*2/3);
        }];
        [_arrowButton setImage:[UIImage imageNamed:@"buddy_header_arrow.png"] forState:UIControlStateNormal];
        [_arrowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        /** 设置Button按钮内容的内边距*/
        [_arrowButton setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        /** 设置Button内容的位置居左*/
        [_arrowButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_arrowButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
        /** 设置button图片的位置*/
        [_arrowButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_arrowButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        _arrowButton.imageView.clipsToBounds = NO;
        _arrowButton.titleLabel.font=Font_Number(15);
        [_arrowButton setTitle:@"当前时间" forState:0];
    }
    return _arrowButton;
}


#pragma mark - buttonAction
- (void)buttonAction
{
    self.sectionGroupModel.isOpen=!self.sectionGroupModel.isOpen;
    
    /** 如果代理响应了代理方法，就要调用这个方法*/
    if ([self.delegate respondsToSelector:@selector(didSelectTableViewHeaderFooterView)]) {
        [self.delegate didSelectTableViewHeaderFooterView];
    }
    
}

- (void)didMoveToSuperview
{
    /** 根据isOpen属性判断是否旋转？如果为YES，则旋转90°，否则不旋转*/
    self.arrowButton.imageView.transform = self.sectionGroupModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    /** 注意这里要设置为bounds 不然会出错*/
//        self.arrowButton.frame = self.bounds;
    
}

#pragma mark - groupModel
-(void)setSectionGroupModel:(SectionGroupModel *)sectionGroupModel
{
    _sectionGroupModel=sectionGroupModel;
    [self.arrowButton setTitle:sectionGroupModel.name forState:0];
}


@end
